/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gmovil_jpa.controladores;

import gmovil_jpa.controladores.exceptions.IllegalOrphanException;
import gmovil_jpa.controladores.exceptions.NonexistentEntityException;
import gmovil_jpa.entidades.Articulo;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import gmovil_jpa.entidades.Remision;
import gmovil_jpa.entidades.Modelo;
import gmovil_jpa.entidades.DetalleFacturaArticulo;
import java.util.ArrayList;
import java.util.List;
import gmovil_jpa.entidades.ValorAtributoArticulo;

/**
 *
 * @author castlerock
 */
public class ArticuloJpaController {

    public ArticuloJpaController() {
        emf = Persistence.createEntityManagerFactory("gmovil_jpaPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Articulo articulo) {
        if (articulo.getDetalleFacturaArticuloList() == null) {
            articulo.setDetalleFacturaArticuloList(new ArrayList<DetalleFacturaArticulo>());
        }
        if (articulo.getValorAtributoArticuloList() == null) {
            articulo.setValorAtributoArticuloList(new ArrayList<ValorAtributoArticulo>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Remision remision = articulo.getRemision();
            if (remision != null) {
                remision = em.getReference(remision.getClass(), remision.getIdRemision());
                articulo.setRemision(remision);
            }
            Modelo modelo = articulo.getModelo();
            if (modelo != null) {
                modelo = em.getReference(modelo.getClass(), modelo.getIdModelo());
                articulo.setModelo(modelo);
            }
            List<DetalleFacturaArticulo> attachedDetalleFacturaArticuloList = new ArrayList<DetalleFacturaArticulo>();
            for (DetalleFacturaArticulo detalleFacturaArticuloListDetalleFacturaArticuloToAttach : articulo.getDetalleFacturaArticuloList()) {
                detalleFacturaArticuloListDetalleFacturaArticuloToAttach = em.getReference(detalleFacturaArticuloListDetalleFacturaArticuloToAttach.getClass(), detalleFacturaArticuloListDetalleFacturaArticuloToAttach.getIdDetalleFacturaArticulo());
                attachedDetalleFacturaArticuloList.add(detalleFacturaArticuloListDetalleFacturaArticuloToAttach);
            }
            articulo.setDetalleFacturaArticuloList(attachedDetalleFacturaArticuloList);
            List<ValorAtributoArticulo> attachedValorAtributoArticuloList = new ArrayList<ValorAtributoArticulo>();
            for (ValorAtributoArticulo valorAtributoArticuloListValorAtributoArticuloToAttach : articulo.getValorAtributoArticuloList()) {
                valorAtributoArticuloListValorAtributoArticuloToAttach = em.getReference(valorAtributoArticuloListValorAtributoArticuloToAttach.getClass(), valorAtributoArticuloListValorAtributoArticuloToAttach.getIdValorAtributoArticulo());
                attachedValorAtributoArticuloList.add(valorAtributoArticuloListValorAtributoArticuloToAttach);
            }
            articulo.setValorAtributoArticuloList(attachedValorAtributoArticuloList);
            em.persist(articulo);
            if (remision != null) {
                remision.getArticuloList().add(articulo);
                remision = em.merge(remision);
            }
            if (modelo != null) {
                modelo.getArticuloList().add(articulo);
                modelo = em.merge(modelo);
            }
            for (DetalleFacturaArticulo detalleFacturaArticuloListDetalleFacturaArticulo : articulo.getDetalleFacturaArticuloList()) {
                Articulo oldArticuloOfDetalleFacturaArticuloListDetalleFacturaArticulo = detalleFacturaArticuloListDetalleFacturaArticulo.getArticulo();
                detalleFacturaArticuloListDetalleFacturaArticulo.setArticulo(articulo);
                detalleFacturaArticuloListDetalleFacturaArticulo = em.merge(detalleFacturaArticuloListDetalleFacturaArticulo);
                if (oldArticuloOfDetalleFacturaArticuloListDetalleFacturaArticulo != null) {
                    oldArticuloOfDetalleFacturaArticuloListDetalleFacturaArticulo.getDetalleFacturaArticuloList().remove(detalleFacturaArticuloListDetalleFacturaArticulo);
                    oldArticuloOfDetalleFacturaArticuloListDetalleFacturaArticulo = em.merge(oldArticuloOfDetalleFacturaArticuloListDetalleFacturaArticulo);
                }
            }
            for (ValorAtributoArticulo valorAtributoArticuloListValorAtributoArticulo : articulo.getValorAtributoArticuloList()) {
                Articulo oldArticuloOfValorAtributoArticuloListValorAtributoArticulo = valorAtributoArticuloListValorAtributoArticulo.getArticulo();
                valorAtributoArticuloListValorAtributoArticulo.setArticulo(articulo);
                valorAtributoArticuloListValorAtributoArticulo = em.merge(valorAtributoArticuloListValorAtributoArticulo);
                if (oldArticuloOfValorAtributoArticuloListValorAtributoArticulo != null) {
                    oldArticuloOfValorAtributoArticuloListValorAtributoArticulo.getValorAtributoArticuloList().remove(valorAtributoArticuloListValorAtributoArticulo);
                    oldArticuloOfValorAtributoArticuloListValorAtributoArticulo = em.merge(oldArticuloOfValorAtributoArticuloListValorAtributoArticulo);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Articulo articulo) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Articulo persistentArticulo = em.find(Articulo.class, articulo.getIdArticulo());
            Remision remisionOld = persistentArticulo.getRemision();
            Remision remisionNew = articulo.getRemision();
            Modelo modeloOld = persistentArticulo.getModelo();
            Modelo modeloNew = articulo.getModelo();
            List<DetalleFacturaArticulo> detalleFacturaArticuloListOld = persistentArticulo.getDetalleFacturaArticuloList();
            List<DetalleFacturaArticulo> detalleFacturaArticuloListNew = articulo.getDetalleFacturaArticuloList();
            List<ValorAtributoArticulo> valorAtributoArticuloListOld = persistentArticulo.getValorAtributoArticuloList();
            List<ValorAtributoArticulo> valorAtributoArticuloListNew = articulo.getValorAtributoArticuloList();
            List<String> illegalOrphanMessages = null;
            for (DetalleFacturaArticulo detalleFacturaArticuloListOldDetalleFacturaArticulo : detalleFacturaArticuloListOld) {
                if (!detalleFacturaArticuloListNew.contains(detalleFacturaArticuloListOldDetalleFacturaArticulo)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain DetalleFacturaArticulo " + detalleFacturaArticuloListOldDetalleFacturaArticulo + " since its articulo field is not nullable.");
                }
            }
            for (ValorAtributoArticulo valorAtributoArticuloListOldValorAtributoArticulo : valorAtributoArticuloListOld) {
                if (!valorAtributoArticuloListNew.contains(valorAtributoArticuloListOldValorAtributoArticulo)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain ValorAtributoArticulo " + valorAtributoArticuloListOldValorAtributoArticulo + " since its articulo field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            if (remisionNew != null) {
                remisionNew = em.getReference(remisionNew.getClass(), remisionNew.getIdRemision());
                articulo.setRemision(remisionNew);
            }
            if (modeloNew != null) {
                modeloNew = em.getReference(modeloNew.getClass(), modeloNew.getIdModelo());
                articulo.setModelo(modeloNew);
            }
            List<DetalleFacturaArticulo> attachedDetalleFacturaArticuloListNew = new ArrayList<DetalleFacturaArticulo>();
            for (DetalleFacturaArticulo detalleFacturaArticuloListNewDetalleFacturaArticuloToAttach : detalleFacturaArticuloListNew) {
                detalleFacturaArticuloListNewDetalleFacturaArticuloToAttach = em.getReference(detalleFacturaArticuloListNewDetalleFacturaArticuloToAttach.getClass(), detalleFacturaArticuloListNewDetalleFacturaArticuloToAttach.getIdDetalleFacturaArticulo());
                attachedDetalleFacturaArticuloListNew.add(detalleFacturaArticuloListNewDetalleFacturaArticuloToAttach);
            }
            detalleFacturaArticuloListNew = attachedDetalleFacturaArticuloListNew;
            articulo.setDetalleFacturaArticuloList(detalleFacturaArticuloListNew);
            List<ValorAtributoArticulo> attachedValorAtributoArticuloListNew = new ArrayList<ValorAtributoArticulo>();
            for (ValorAtributoArticulo valorAtributoArticuloListNewValorAtributoArticuloToAttach : valorAtributoArticuloListNew) {
                valorAtributoArticuloListNewValorAtributoArticuloToAttach = em.getReference(valorAtributoArticuloListNewValorAtributoArticuloToAttach.getClass(), valorAtributoArticuloListNewValorAtributoArticuloToAttach.getIdValorAtributoArticulo());
                attachedValorAtributoArticuloListNew.add(valorAtributoArticuloListNewValorAtributoArticuloToAttach);
            }
            valorAtributoArticuloListNew = attachedValorAtributoArticuloListNew;
            articulo.setValorAtributoArticuloList(valorAtributoArticuloListNew);
            articulo = em.merge(articulo);
            if (remisionOld != null && !remisionOld.equals(remisionNew)) {
                remisionOld.getArticuloList().remove(articulo);
                remisionOld = em.merge(remisionOld);
            }
            if (remisionNew != null && !remisionNew.equals(remisionOld)) {
                remisionNew.getArticuloList().add(articulo);
                remisionNew = em.merge(remisionNew);
            }
            if (modeloOld != null && !modeloOld.equals(modeloNew)) {
                modeloOld.getArticuloList().remove(articulo);
                modeloOld = em.merge(modeloOld);
            }
            if (modeloNew != null && !modeloNew.equals(modeloOld)) {
                modeloNew.getArticuloList().add(articulo);
                modeloNew = em.merge(modeloNew);
            }
            for (DetalleFacturaArticulo detalleFacturaArticuloListNewDetalleFacturaArticulo : detalleFacturaArticuloListNew) {
                if (!detalleFacturaArticuloListOld.contains(detalleFacturaArticuloListNewDetalleFacturaArticulo)) {
                    Articulo oldArticuloOfDetalleFacturaArticuloListNewDetalleFacturaArticulo = detalleFacturaArticuloListNewDetalleFacturaArticulo.getArticulo();
                    detalleFacturaArticuloListNewDetalleFacturaArticulo.setArticulo(articulo);
                    detalleFacturaArticuloListNewDetalleFacturaArticulo = em.merge(detalleFacturaArticuloListNewDetalleFacturaArticulo);
                    if (oldArticuloOfDetalleFacturaArticuloListNewDetalleFacturaArticulo != null && !oldArticuloOfDetalleFacturaArticuloListNewDetalleFacturaArticulo.equals(articulo)) {
                        oldArticuloOfDetalleFacturaArticuloListNewDetalleFacturaArticulo.getDetalleFacturaArticuloList().remove(detalleFacturaArticuloListNewDetalleFacturaArticulo);
                        oldArticuloOfDetalleFacturaArticuloListNewDetalleFacturaArticulo = em.merge(oldArticuloOfDetalleFacturaArticuloListNewDetalleFacturaArticulo);
                    }
                }
            }
            for (ValorAtributoArticulo valorAtributoArticuloListNewValorAtributoArticulo : valorAtributoArticuloListNew) {
                if (!valorAtributoArticuloListOld.contains(valorAtributoArticuloListNewValorAtributoArticulo)) {
                    Articulo oldArticuloOfValorAtributoArticuloListNewValorAtributoArticulo = valorAtributoArticuloListNewValorAtributoArticulo.getArticulo();
                    valorAtributoArticuloListNewValorAtributoArticulo.setArticulo(articulo);
                    valorAtributoArticuloListNewValorAtributoArticulo = em.merge(valorAtributoArticuloListNewValorAtributoArticulo);
                    if (oldArticuloOfValorAtributoArticuloListNewValorAtributoArticulo != null && !oldArticuloOfValorAtributoArticuloListNewValorAtributoArticulo.equals(articulo)) {
                        oldArticuloOfValorAtributoArticuloListNewValorAtributoArticulo.getValorAtributoArticuloList().remove(valorAtributoArticuloListNewValorAtributoArticulo);
                        oldArticuloOfValorAtributoArticuloListNewValorAtributoArticulo = em.merge(oldArticuloOfValorAtributoArticuloListNewValorAtributoArticulo);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = articulo.getIdArticulo();
                if (findArticulo(id) == null) {
                    throw new NonexistentEntityException("The articulo with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Articulo articulo;
            try {
                articulo = em.getReference(Articulo.class, id);
                articulo.getIdArticulo();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The articulo with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            List<DetalleFacturaArticulo> detalleFacturaArticuloListOrphanCheck = articulo.getDetalleFacturaArticuloList();
            for (DetalleFacturaArticulo detalleFacturaArticuloListOrphanCheckDetalleFacturaArticulo : detalleFacturaArticuloListOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Articulo (" + articulo + ") cannot be destroyed since the DetalleFacturaArticulo " + detalleFacturaArticuloListOrphanCheckDetalleFacturaArticulo + " in its detalleFacturaArticuloList field has a non-nullable articulo field.");
            }
            List<ValorAtributoArticulo> valorAtributoArticuloListOrphanCheck = articulo.getValorAtributoArticuloList();
            for (ValorAtributoArticulo valorAtributoArticuloListOrphanCheckValorAtributoArticulo : valorAtributoArticuloListOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Articulo (" + articulo + ") cannot be destroyed since the ValorAtributoArticulo " + valorAtributoArticuloListOrphanCheckValorAtributoArticulo + " in its valorAtributoArticuloList field has a non-nullable articulo field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Remision remision = articulo.getRemision();
            if (remision != null) {
                remision.getArticuloList().remove(articulo);
                remision = em.merge(remision);
            }
            Modelo modelo = articulo.getModelo();
            if (modelo != null) {
                modelo.getArticuloList().remove(articulo);
                modelo = em.merge(modelo);
            }
            em.remove(articulo);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Articulo> findArticuloEntities() {
        return findArticuloEntities(true, -1, -1);
    }

    public List<Articulo> findArticuloEntities(int maxResults, int firstResult) {
        return findArticuloEntities(false, maxResults, firstResult);
    }

    private List<Articulo> findArticuloEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Articulo.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Articulo findArticulo(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Articulo.class, id);
        } finally {
            em.close();
        }
    }

    public int getArticuloCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Articulo> rt = cq.from(Articulo.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
